<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */ 
        $session = new Zend_Session_Namespace('id_persona');    
        $id_persona   = $session->id_persona;
        $this->view->id_persona = $id_persona;
        $this->view->top_persona = $this->view->getInfoPersona($id_persona);
    }

    public function indexAction()
    {
        // action body
        $auth = Zend_Auth::getInstance();  
        if ($auth->hasIdentity()) {
            $id = $auth->getIdentity()->id_utente;

            $admin = new Application_Model_DbTable_Administrator();
            $this->view->admin = $admin->getAdmin($id);
            
            // action body
            $form = new Application_Form_RicercaPersone();
            // richiesta del form
            $request= $this->getRequest();

            if ($request->isPost()) {
                        if ($form->isValid($request->getPost())) {

                            $persona = $this->_process_ricerca_persona($form->getValues());
                            if ($persona) {
                                //$this->_helper->redirector('ricerca-persone-success', 'Index');
                                $this->view->persone = $persona;
                            }else{
                                $this->view->notifica= '<p style="padding:2px;">Ops, si è verificato un errore.</P>';
                                //$this->_helper->redirector->gotoRoute('ricerca-persone','Index');
                            }
                        }
                    }
            $this->view->form = $form; 

        } else {
            $this->_forward('index', 'Auth');

        }
    }


    protected function _process_ricerca_persona($values)
    {
        $text = $values['searchtesto'];
        $persona = false;        
        try{

            require_once APPLICATION_PATH . '/models/Db/Persone_db.php';
            $db_obj = new Persone_db();  
            $persona = $db_obj->searchPersona($text);

        } catch (Zend_Mail_Exception $e){
            echo $e->getMessage();
            return false;
        }
        
        return $persona;
    }


    public function searchAction()
    {
        // action body
        $this->_helper->_layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);        
        
        require_once APPLICATION_PATH . '/models/Db/Lib_db.php';
        $db_obj = new Lib_db();              

        $q     = stripcslashes($this->_request->getParam('q'));
        $table = stripcslashes($this->_request->getParam('table'));        
        
        if(!empty($q)) {
            $result = $db_obj->search($q,$table);
        }
        echo json_encode($result);
    }
    
    public function searchNormeCategorieAction() {
        $this->_helper->_layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);                
        
        require_once APPLICATION_PATH . '/models/Db/Lib_db.php';
        $lib_obj = new Lib_db();              

        $q  = stripcslashes($this->_request->getParam('categoria')); 
        if(!empty($q)) {
            $result = $lib_obj->getNormeByCategoria($q);
        }
        echo json_encode($result);        
    }
    
    
    public function searchNormaAction() {
        $this->_helper->_layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);                
        
        require_once APPLICATION_PATH . '/models/Db/Lib_db.php';
        $lib_obj = new Lib_db();              

        $q  = $this->_request->getParam('id');
        if(!empty($q)) {
            $result = $lib_obj->getNorma($q);
        }
        echo json_encode($result);        
    }    


}






